//
//  KAPwdView.swift
//  kano
//
//  Created by Sophia on 2024/8/20.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa

class KAPwdView: UIView {
    
    var editingBlock: ((KAPwdView, String) -> Void)?
    var editDidEndBlock: ((KAPwdView, KATFModel) -> Void)?
    
    private let disposeBag = DisposeBag()
    private var placeHolder: String = ""
    private var limitInputCount: Int = 6
    private var lastDate: Date?
    
    lazy var contentTF: UITextField = {
        let contentTF = UITextField()
        contentTF.font = UIFont.systemFont(ofSize: kscale(12), weight: .medium)
        
        let holderText = placeHolder.isEmpty ? "" : placeHolder
        let placeholder = NSMutableAttributedString(string: holderText)
        placeholder.addAttributes([
            .font: UIFont.systemFont(ofSize: kscale(16)),
            .foregroundColor: UIColor.color77777D
        ], range: NSRange(location: 0, length: holderText.count))
        contentTF.attributedPlaceholder = placeholder
        contentTF.textColor = .black
        contentTF.isSecureTextEntry = true
        contentTF.keyboardType = .default;
        return contentTF
    }()
    
    private lazy var backView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.colorF0F2F5
        view.layer.masksToBounds = true
        view.layer.cornerRadius = kscale(10)
        return view
    }()
    
    lazy var rightBtn: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "eye_unvisible"), for: .normal)
        button.setImage(UIImage(named: "eye_visible"), for: .selected)
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    init(placeHolder: String, limit inputCount: Int = 6) {
        self.placeHolder = placeHolder
        self.limitInputCount = inputCount
        super.init(frame: .zero)
        setUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        backView.snp.makeConstraints { make in
            make.top.left.bottom.right.equalTo(self)
        }
        
        rightBtn.snp.makeConstraints { make in
            make.right.equalTo(backView).offset(-kscale(8))
            make.centerY.equalTo(backView)
            make.size.equalTo(CGSize(width: kscale(34), height: kscale(34)))
        }
        
        contentTF.snp.makeConstraints { make in
            make.left.equalTo(backView).offset(kscale(8))
            make.right.equalTo(rightBtn.snp.left).offset(-kscale(10))
            make.centerY.equalTo(backView)
        }
    }
    
    func callKeyBoard() {
        contentTF.becomeFirstResponder()
    }
}

// UIColor extension for hex string
extension KAPwdView {
    private func setUI() {
        addSubview(backView)
        backView.addSubview(rightBtn)
        backView.addSubview(contentTF)
        bindUI()
        layoutSubviews()
    }
    func bindUI() {
//        contentTF.rx.text
//            .orEmpty
//            .subscribe(onNext: { text in
//                //print("Text changed: \(text)")
//            })
//            .disposed(by: disposeBag)
        
        // editingDidBegin
        contentTF.rx.controlEvent(.editingDidBegin)
            .subscribe(onNext: {
                //print("Editing began")
            })
            .disposed(by: disposeBag)
        // editingChanged
        contentTF.rx.controlEvent(.editingChanged)
            .subscribe(onNext: {
                //print("Editing Changed")
            })
            .disposed(by: disposeBag)
        // editingDidEnd
        contentTF.rx.controlEvent(.editingDidEnd)
            .subscribe(onNext: {
                //print("Editing ended")
            })
            .disposed(by: disposeBag)
        
        contentTF.rx.text
            .orEmpty
            .map { [self] text -> String in
                let pwd = text.verifyPwdInput ? text : String(text.dropLast())
                return pwd.count > limitInputCount ? String(pwd.prefix(limitInputCount)) : pwd
            }
            .bind(to: contentTF.rx.text)
            .disposed(by: disposeBag)
        
        rightBtn.rx.tap
            .subscribe(onNext: { [weak self] in
                self?.contentTF.isSecureTextEntry.toggle()
                self?.rightBtn.isSelected.toggle()
            })
            .disposed(by: disposeBag)
        
    }
}






